﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testing_thing
{
    public class TransferCommand : Command
    {
        public TransferCommand() : base(new string[] { "pickup", "drop", "put", "take" }) { }

        public override string Run(Player p, string[] input)
        {
            if (input.Length == 2 && input[0] == "pickup") return DoPickup(p, input[1]);
            else if (input.Length == 2 && input[0] == "drop") return DoDrop(p, input[1]);
            else if (input.Length == 4 && input[0] == "put" && input[2] == "in") return DoPutIn(p, input[1], input[3]);
            else if (input.Length == 4 && input[0] == "take" && input[2] == "from") return DoTakeFrom(p, input[1], input[3]);

            return "I dont know how to transfer like that";
        }

        private string DoPickup(Player p, string id)
        {
            Item itm = p.InLocation.Inventory.AccessItem(id);
            if (itm == null)
                return "There is nothing ot pickup";
            else if (itm.IsStationary)
            {
                return "I cannot pick that up";
            }
            p.InLocation.Inventory.TakeItem(id);
            p.Inventory.AddItem(itm);

            return "You have added " + itm.Title + " to your inventory.";

        }

        private string DoTakeFrom(Player p, string id, string idContainer)
        {
            GameEntity container = p.LocateEntity(idContainer);
            if (container == null)
                return "There is no such item to take from";
            Item itm = container.Inventory.TakeItem(id);
            p.Inventory.AddItem(itm);

            return "You just took " + itm.Title + " from " + container.Title;

        }

        private string DoPutIn(Player p, string id, string idContainer)
        {
            GameEntity container = p.LocateEntity(idContainer);
            Item itm = p.Inventory.AccessItem(id);
            if (container == null)
            {
                return "Cannot find " + idContainer;
            }
            if (itm == null)
            {
                return "Cannot find " + id;
            }
            if (id == idContainer)
            {
                return "You dum dum! you can't put a " + id + " in itself!";
            }
            else
            {
                itm = p.Inventory.TakeItem(id);
                container.Inventory.AddItem(itm);
                return itm.Title + " was put in " + container.Title;
            }

        }

        private string DoDrop(Player p, string id)
        {
            Item itm = p.Inventory.TakeItem(id);
            if (itm == null)
                return " There is no such item to drop";
            p.InLocation.Inventory.AddItem(itm);
            return "You just dropped " + itm.Title;
        }


    }
}
